<?php
class Distributor extends CI_Controller {
	
	public function __construct() {
        parent::__construct();
        $this->load->model('mdistributor');
		$this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('encrypt');
        $this->load->library('enc');
        $this->load->model('mmaster');
        $this->load->model('mglobal');
    }
	
	########################################################################
    ########################################################################

    public function getById($id) {
        if (isset($id))
            echo json_encode($this->mmaster->getAgents($id));
    }

    public function read( $kindOfRecord="" ) {
		//echo json_encode( $this->mdistributor->getDistributor( $kindOfRecord ) );
		$result = $this->mdistributor->getDistributor( $kindOfRecord ) ;

        //encode
        for($i=0;$i<count($result);$i++){
    		$result[$i]->distributor_id = $this->enc->fogit($result[$i]->distributor_id);
    	}
        
        echo json_encode($result);
	}
	
	function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }
	
	public function update( $kindOfRecord, $id )
	{
		$this->magents->updateAgent( $id );
	}
	
	public function delete( $id = null ) {
		if( is_null( $id ) ) {
			echo 'ERROR: Id not provided.';
			return;
		}
		
		$this->mdistributor->delete( $id );
		echo 'Data berhasil dihapus.';
			return;
		//$this->session->set_flashdata('message', 'Data berhasil dihapus');
		
		//redirect(base_url()."index.php/master/agents");
	}
	
	
	################
	##function untuk menampilkan view
	
	function index($proses="",$id="")
	{

		//decode
		$id = $this->enc->defog($id);
		$data['template'] 	= "shell/smooth";
		$data['users']    	= $this->mdistributor->get();
		$data['main_view']	= 'f-distributor';
		$data['proses']		= $proses;
		$data['id']			= $id;
		
		$data['data'] = array("kode"=>"","regional"=>"","nama"=>"","alamat"=>"");
		if($proses<>"" && $id<>""){
		    $getdata = $this->mdistributor->get($id);
		    foreach($getdata as $r){
				$data['data']['kode'] 		= $r->original_distributor_code;
				$data['data']['regional'] 		= $r->original_regional_name;
				$data['data']['nama'] 		= $r->distributor_name;
				$data['data']['alamat'] 		= $r->distributor_address;
		    }
		}
		#inisialisasi elemen form yang harus diisi
		$this->form_validation->set_rules('name', 'Group Name', 'required');
		
		$this->form_validation->set_error_delimiters('<li>','</li>');
		$this->form_validation->set_message('required', 'Kolom <b>%s</b> harus diisi.');
                
		if ($this->form_validation->run() != FALSE){
			// masukkan data di sini
			$this->mdistributor->save($id);
			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			redirect(base_url()."index.php/group_management");
		}	
		$hasil = "";
		$this->load->view($data['template'],$data);
	}
}
?>